using B.S.Data.MES.API.Applications.BaseData.Command.MaterialGroupCommand;
using B.S.Domain.BasicData.MaterialManagement.MaterialGroups;
using B.S.Domain.BasicData.MaterialManagement.Material_type;
using B.S.Domain.Process.ProcessrouteName;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using B.S.Domain.Dto.WuLiaoDto;

namespace B.S.Data.MES.API.Applications.BaseData.CommandHandlers.MaterialGroupCommandHandlers
{
    public class QueryMaterialGroupCommandHandler : IRequestHandler<QueryMaterialGroupCommand,  APIRequst< APIPageing<List<GropDto>>>>
    {
        private readonly IBaseRepository<Material_group> _materialGroupRepository;
        private readonly IBaseRepository<Material_type> _materialTypeRepository;
        private readonly IBaseRepository<Processroute> _processrouteRepository;

        public QueryMaterialGroupCommandHandler(
            IBaseRepository<Material_group> materialGroupRepository,
            IBaseRepository<Material_type> materialTypeRepository,
            IBaseRepository<Processroute> processrouteRepository)
        {
            _materialGroupRepository = materialGroupRepository;
            _materialTypeRepository = materialTypeRepository;
            _processrouteRepository = processrouteRepository;
        }

        public  Task<APIRequst<APIPageing<List<GropDto>>>> Handle(QueryMaterialGroupCommand request, CancellationToken cancellationToken)
        {
            var list = from a in _materialGroupRepository.GetAll()
                       join b in _materialTypeRepository.GetAll() on a.Materialtypeid equals b.Materialtypeid
                       join c in _processrouteRepository.GetAll() on a.Id equals c.Id into processrouteJoin
                       from c in processrouteJoin.DefaultIfEmpty()
                       select new GropDto 
                       {
                                Id=a.Id,
                                Materialcode=a.Materialcode,
                                Materialdescription=a.Materialdescription,
                                Materialname=a.Materialname,
                                Materialtypeid=a.Materialtypeid,
                                MaterialtypeName=b.MaterialtypeName,
                                ProcessrouteName=c.ProcessrouteName,
                                MaterId=a.MaterId,
                                CreateTime=a.CreateTime,
                                CreateName=a.CreateName, UpdateTime=a.UpdateTime,
                       };
            APIPageing<List<GropDto>> Page = new APIPageing<List<GropDto>>();
            Page.TotalCount = list.Count();
            Page.Total = (int)Math.Ceiling((1.0 * Page.TotalCount) / request.PageSize);
            Page.Data = list.OrderBy(x => x.MaterId).Skip((request.Pageindex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return Task.FromResult(new APIRequst<APIPageing<List<GropDto>>>
            {
                Code = RequstCode.成功,
                Msg = RequstCode.成功.ToString(),
                Data = Page
            });
        }
    }
}
